<template>
  <div class="app-container">
    <template>
      <div class="right-detail">
        <!-- 搜索框 -->
        <div class="cbox">
          <el-form class="form" :model="flt" :inline="true" ref="fltForm">
            <el-form-item size="mini" label="标的编号：" prop="code">
              <el-input clearable v-model="queryParam.code" size="mini" style="width: 100%"></el-input>
            </el-form-item>
            <el-form-item size="mini" label="状态：" prop="status">
              <el-select
                clearable
                v-model="queryParam.status"
                style="width: 100%"
                placeholder="请选择"
              >
                <el-option
                  v-for="(item, index) in bidStatusDataList"
                  :key="index"
                  :value="item.rvLowValue"
                  :label="item.rvCmeaning"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item size="mini">
              <el-button size="mini" type="primary" @click="initMTable">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
        <split-pane split="horizontal" v-on:resize="resize" :min-percent="20" :default-percent="55">
          <!-- 主表 -->
          <template slot="paneL">
            <div class="top-type" ref="topType">
              <div class="btn-area" ref="btnArea">
                <buttonName @clickEvent="clickEvent"></buttonName>
                <!--                <el-button-->
                <!--                  v-if="isAuth('gen')"-->
                <!--                  size="mini"-->
                <!--                  type="primary"-->
                <!--                  plain-->
                <!--                  @click="genDialog"-->
                <!--                >汇总需求-->
                <!--                </el-button>-->
                <!--                <el-button v-if="isAuth('edit')" size="mini" type="primary" plain @click="edit">编辑</el-button>-->
                <!--                <el-button v-if="isAuth('del')" size="mini" type="primary" plain @click="del">删除</el-button>-->
              </div>
              <div>
                <el-table
                  :data="tableDomain.mTable.data"
                  style="width: 100%"
                  v-loading="tableDomain.mTable.loading"
                  @selection-change="handleSelectionChange"
                  @row-click="mRowClick"
                  highlight-current-row
                  :height="typeHeight"
                  border
                >
                  <el-table-column align="center" width="50" type="index" label="序号"></el-table-column>
                  <el-table-column type="selection" width="55"></el-table-column>
                  <el-table-column align="center" prop="bidStatusName" label="状态"></el-table-column>
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    prop="bidCode"
                    label="标的编号"
                    width="200"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    prop="bidTypeName"
                    label="运输类型"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    prop="bidName"
                    label="标的名称"
                    width="200"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    prop="bidAmount"
                    label="运输总量(吨)"
                    width="130"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    prop="bidQuotationsName"
                    label="竞价方式"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    prop="bidOffer"
                    label="单价竞价范围(元)"
                    width="140"
                  ></el-table-column>
                  <el-table-column
                    align="center"
                    show-overflow-tooltip
                    prop="bidEdited"
                    width="200"
                    label="操作时间"
                  ></el-table-column>
                  <el-table-column show-overflow-tooltip align="center" prop="bidRemark" label="备注"></el-table-column>
                </el-table>
                <el-pagination
                  @size-change="mcs"
                  @current-change="mci"
                  :current-page.sync="tableDomain.mTable.pageInfo.pageIndex"
                  :page-sizes="[30, 60, 90, 120]"
                  :page-size="tableDomain.mTable.pageInfo.pageSize"
                  layout="sizes, prev, pager, next, jumper, total"
                  :total="tableDomain.mTable.pageInfo.total"
                ></el-pagination>
              </div>
            </div>
          </template>
          <!-- 子表 -->
          <template slot="paneR">
            <div class="codeNumberCode" style="background-color: #fff;">需求信息</div>
            <div class="bottom-code" ref="bottomCode" style="background-color: #fff;">
              <el-table
                :data="tableDomain.sTable.data"
                style="width: 100%"
                :height="detlHeight"
                v-loading="tableDomain.sTable.loading"
                border
              >
                <el-table-column
                  show-overflow-tooltip
                  align="center"
                  width="50"
                  type="index"
                  label="序号"
                ></el-table-column>
                <el-table-column
                  show-overflow-tooltip
                  align="center"
                  prop="demandCode"
                  label="需求编号"
                  width="200"
                ></el-table-column>
                <el-table-column show-overflow-tooltip align="center" prop="brand" label="货物品牌"></el-table-column>
                <el-table-column show-overflow-tooltip align="center" prop="dailyOutput" label="规格"></el-table-column>
                <el-table-column show-overflow-tooltip align="center" prop="product" label="产品大类"></el-table-column>
                <el-table-column
                  show-overflow-tooltip
                  align="center"
                  prop="kind"
                  label="品种"
                  width="150"
                ></el-table-column>
                <el-table-column
                  show-overflow-tooltip
                  align="center"
                  prop="transportType"
                  label="运输类型"
                ></el-table-column>
                <el-table-column
                  show-overflow-tooltip
                  align="center"
                  prop="transportMode"
                  label="运输方式"
                ></el-table-column>
                <el-table-column
                  show-overflow-tooltip
                  align="center"
                  prop="transportQuantity"
                  label="运输数量(吨)"
                  width="130"
                ></el-table-column>
                <el-table-column
                  show-overflow-tooltip
                  align="center"
                  prop="sfdSubmitTime"
                  label="提交时间"
                  width="300"
                ></el-table-column>
                <el-table-column show-overflow-tooltip align="center" prop="dptName" label="需求来源"></el-table-column>
                <el-table-column show-overflow-tooltip align="center" prop="remark" label="备注"></el-table-column>
              </el-table>
            </div>
          </template>
        </split-pane>
      </div>
    </template>

    <!-- 需求汇总dialog -->
    <biddingDialog :info="dialogText"></biddingDialog>
  </div>
</template>

<script>
import isAuth from '@/mixins/buttonAuth'
import resizeMixin from '@/mixins/resize'
import biddingDialog from '../threeSummary/dialog'
import {bidDropDown, bidList, bidListDetail, delBid, queryDemandLine, queryHistory, refuseDemand, updateBidBatch} from '@/request/api'
import store from '@/store'
import buttonName from '@/components/buttonName/button-name'

export default {
  mixins: [resizeMixin, isAuth],
  data() {
    return {
      transportationType: [], // 运输类型下拉框
      BoHcount: '',
      selectAllArray: [], //选中所有的数据
      selNoApproveArray: [], //未审核的数据
      selApproveArray: [], //已审核的数据
      tranShow: true,
      refuseShow: true,
      refuseDialog: {
        show: false,
        title: '',
        currentData: {}
      },
      refuseFrom: {
        data: {
          refuseMsg: ''
        },
        rules: {
          refuseMsg: [
            {required: true, message: '请输入驳回意见', trigger: 'blur'}
          ]
        }
      },
      typeHeight: -1,
      detlHeight: -1,
      bidTypeDataList: [],
      bidStatusDataList: [],
      bidQuotations: [],
      transportModeList: [],
      kindModeList: [],
      productModeList: [],
      sugarModeList: [],
      queryParam: {
        code: '', // 标的编号
        status: '' // 标的状态
      },
      tableDomain: {
        mTable: {
          data: [],
          loading: false,
          currentData: {},
          pageInfo: {
            pageIndex: 1,
            pageSize: 30,
            total: 0
          }
        },
        sTable: {
          data: [],
          loading: false
        },
        dTable: {
          data: [],
          loading: false,
          queryParam: {
            code: '',
            transportMode: '',
            tranType: ''
          },
          selectChangeData: [],
          selectBoxShow: true,
          pageInfo: {
            pageIndex: 1,
            pageSize: 10,
            total: 0
          }
        }
      },
      dialogFrom: {
        diable: {
          allDisable: false
        },
        data: {
          bidAmount: 0,
          bidCode: '',
          bidName: '',
          bidQuotations: '',
          bidRange: '',
          bidRemark: '',
          bidPlatformTogether: 'N',
          bidOfferStart: '',
          bidOfferEnd: '',
          bidType: 'ZJZY'
        },
        rules: {
          bidCode: [
            {required: true, message: '输入标的编号', trigger: 'blur'}
          ],
          bidType: [
            {required: true, message: '请选择运输类型', trigger: 'change'}
          ],
          bidName: [
            {required: true, message: '请输入标的名称', trigger: 'blur'}
          ],
          bidPlatformTogether: [
            {required: true, message: '请选择是否平台合作', trigger: 'blur'}
          ]
        }
      },
      dialogText: {
        title: '',
        show: false,
        button: {
          t: '',
          f: ''
        },
        selectBoxShow: false,
        qeuryButtonShow: false,
        paginationShow: false,
        refuseShow: true,
        historyShow: false,
        submitTimeShow: true,
        sugarModeListShow: true,
        currentData: {},
        sugarModeListName: '',
        source: 3,
        topFormShow: true,
        groupShow: true
      },
      tranDialogInfo: {
        title: '',
        show: false,
        table: {
          data: []
        }
      },
      HisDialogInfo: {
        title: '',
        show: false,
        table: {
          data: []
        }
      },
      genName: {
        dptName: '',
        typeName: '',
        code: ''
      }
    }
  },
  components: {
    biddingDialog,
    buttonName
  },
  created() {
    this.resize = () => {
      this.typeHeight = this.$refs.topType.offsetHeight - 80
      this.detlHeight = this.$refs.bottomCode.offsetHeight - 80
    }
  },
  mounted() {
    this.initMTable()
    this.initBidType()
    this.initBidStatus()
    this.initBidQuotations()
    this.initTransportModeList()
    this.initKindList()
    this.initProductList()
    this.initSugarList()
  },
  methods: {
    // 获取当前选中行
    handleSelectionChange(val) {
      this.selectAllArray = []
      this.selNoApproveArray = []
      this.selApproveArray = []
      if (val.length > 0) {
        for (var i = 0; i < val.length; i++) {
          this.selectAllArray.push(val[i])
          if (val[i].bidStatus == 'TJPT') {
            this.selApproveArray.push(val[i])
          } else {
            this.selNoApproveArray.push(val[i])
          }
        }
      }
    },
    // 初始主列表
    initMTable() {
      let t = this
      t.tableDomain.sTable.data = []
      bidList({
        pageIndex: t.tableDomain.mTable.pageInfo.pageIndex,
        pageSize: t.tableDomain.mTable.pageInfo.pageSize,
        bidCode: t.queryParam.code,
        bidStatus: t.queryParam.status,
        source: 3
      }).then(res => {
        if (res.success == 1) {
          t.tableDomain.mTable.data = res.data
          t.tableDomain.mTable.pageInfo.total = res.total
        }
      })
    },
    initBidType() {
      let t = this
      bidDropDown({
        name: '标的类型'
      }).then(res => {
        if (res.success == 1) {
          t.bidTypeDataList = res.data
          this.transportationType = res.data
        }
      })
    },
    initBidStatus() {
      let t = this
      bidDropDown({
        name: '标书状态',
        rvEdicode: 2
      }).then(res => {
        if (res.success == 1) {
          t.bidStatusDataList = res.data
        }
      })
    },
    // 汇总需求dialog
    genDialog() {
      let t = this
      t.dialogText.title = '汇总需求'
      t.dialogText.button.t = '确 定'
      t.dialogText.button.f = '取 消'
      t.dialogText.paginationShow = true
      t.dialogText.qeuryButtonShow = true
      t.dialogText.refuseShow = true
      t.dialogText.historyShow = true
      t.dialogText.submitTimeShow = true
      t.dialogText.show = true
      t.dialogText.selectBoxShow = true
      t.dialogText.sugarModeListShow = true
      t.dialogText.topFormShow = true
      t.dialogText.groupShow = true
      this.dialogText.sugarModeListName = '糖厂：'
      if (
        localStorage.getItem('IS_SHOW1') &&
        localStorage.getItem('IS_SHOW1') == 5
      ) {
        this.dialogText.sugarModeListName = '需求来源：'
      }
    },
    releaseInfo() {
      let t = this
      if (t.selectAllArray.length != 0) {
        if (t.selApproveArray != 0) {
          t.$message.error('不能重复提交')
          return
        }
        let li = t.selNoApproveArray
        li.forEach(l => {
          l.bidStatus = 'TJPT'
          l.bidEditor = store.state.user.USR_CODE_DX_H
        })
        updateBidBatch({
          list: li
        }).then(res => {
          this.$message.success('提交成功')
          this.initMTable()
        })
      } else {
        t.$message.error('请勾选一条数据')
      }
    },
    // 删除汇总的需求
    del() {
      let t = this
      if (Object.keys(t.tableDomain.mTable.currentData).length != 0) {
        if (t.tableDomain.mTable.currentData.bidStatus != 'PTHZ') {
          t.$message.error('已经提交的无法删除')
          return
        }
        if (this.tableDomain.mTable.currentData.bidAuditstatus && this.tableDomain.mTable.currentData.bidAuditstatus == 'Y') {
          this.$message.error('已经确认的标书无法删除')
          return
        }
        t.$confirm('您确认要删除选定的数据吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(() => {
          delBid({
            bidId: t.tableDomain.mTable.currentData.bidId,
            source: 3
          }).then(res => {
            if (res.success == 1) {
              t.tableDomain.mTable.currentData = {}
              t.$message.success('删除成功')
              t.initMTable()
            }
          })
        })
      } else {
        t.$message.error('请选择一条数据')
      }
    },
    // 取消需求提交
    delReleaseInfo() {
      if (this.selNoApproveArray.length != 0) {
        this.$message.error('未提交的无法取消')
        return
      }
      this.$confirm('您确认要取消提交选定的数据吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ll = this.selectAllArray
        ll.forEach(s => {
          s.bidStatus = 'QXTJ'
          s.bidEditor = store.state.user.USR_CODE_DX_H
        })
        updateBidBatch({
          list: ll
        }).then(res => {
          this.$message.success('取消成功')
          this.initMTable()
        })
      })
    },
    edit() {
      let t = this
      if (Object.keys(t.tableDomain.mTable.currentData).length != 0) {
        if (
          t.tableDomain.mTable.currentData.bidStatus &&
          t.tableDomain.mTable.currentData.bidStatus == 'TJPT'
        ) {
          t.$message.error('已经提交的标书无法编辑')
          return
        }
        if (this.tableDomain.mTable.currentData.bidAuditstatus && this.tableDomain.mTable.currentData.bidAuditstatus == 'Y') {
          this.$message.error('已经确认的标书无法编辑')
          return
        }
        this.dialogText.currentData = this.tableDomain.mTable.currentData
        t.dialogText.title = '编辑'
        t.dialogText.button.t = '确 定'
        t.dialogText.button.f = '取 消'
        t.dialogText.paginationShow = true
        t.dialogText.qeuryButtonShow = true
        this.dialogText.submitTimeShow = true
        this.dialogText.selectBoxShow = true
        t.tableDomain.dTable.selectBoxShow = true
        t.dialogText.show = true
        t.dialogText.sugarModeListShow = true
        if (
          localStorage.getItem('IS_SHOW1') &&
          localStorage.getItem('IS_SHOW1') == 5
        ) {
          this.dialogText.sugarModeListName = '糖厂：'
        }
      } else {
        t.$message.error('请选择一条数据')
      }
    },
    mci(i) {
      this.tableDomain.mTable.pageInfo.pageIndex = i
      this.initMTable()
    },
    mcs(i) {
      this.tableDomain.mTable.pageInfo.pageSize = i
      this.initMTable()
    },

    mRowClick(val) {
      this.tableDomain.mTable.currentData = val
      //
      bidListDetail({
        bidId: val.bidId
      }).then(res => {
        if (res.success == 1) {
          this.tableDomain.sTable.data = res.data
        }
      })
    },
    dSelect(val) {
      this.tableDomain.dTable.selectChangeData = val
      this.dialogFrom.data.bidAmount = 0
      if (val.length != 0) {
        let sum = 0
        val.forEach(e => {
          sum = sum + parseFloat(e.transportQuantity)
        })
        this.dialogFrom.data.bidAmount = sum
      }
    },
    dialogInputNumberStartChange(val) {
      if (val > this.dialogFrom.data.bidOfferEnd) {
        this.dialogFrom.data.bidOfferEnd = val
      }
    },
    dialogInputNumberEndChange(val) {
      if (val < this.dialogFrom.data.bidOfferStart) {
        this.dialogFrom.data.bidOfferStart = val
      }
    },
    // dialog 查看线路详细信息
    showTransportMethod(val) {
      this.tableDomain.dTable.clickData = val
      this.initTranMethodTable()
      this.tranDialogInfo.title = '线路详情'
      this.tranDialogInfo.show = true
    },
    // dialog 查看历史记录
    showHistory(val) {
      this.tableDomain.dTable.clickData = val
      this.initHisMethodTable()
      this.HisDialogInfo.title = '驳回历史'
      this.HisDialogInfo.show = true
    },
    initHisMethodTable() {
      queryHistory({
        demandCode: this.tableDomain.dTable.clickData.demandCode
      }).then(res => {
        if (res.success == 1) {
          this.HisDialogInfo.table.data = res.data.list
          this.BoHcount = res.data.number
        }
      })
    },
    initTranMethodTable() {
      queryDemandLine({
        sugarId: this.tableDomain.dTable.clickData.sugarId
      }).then(res => {
        if (res.success == 1) {
          this.tranDialogInfo.table.data = res.data
        }
      })
    },
    // 驳回需求
    refuse(val) {
      this.refuseDialog.currentData = val
      this.refuseDialog.title = '驳回'
      this.refuseDialog.show = true
    },
    refuseT() {
      this.$refs['refuseFromRef'].validate(valid => {
        if (valid) {
          refuseDemand({
            demandCode: this.refuseDialog.currentData.demandCode,
            sfdRefuseMsg: this.refuseFrom.data.refuseMsg,
            status: 'BOH',
            sugarId: this.refuseDialog.currentData.sugarId
          }).then(res => {
            this.$message.success('驳回成功')
            this.dTableDataList()
            this.refuseF()
          })
        }
      })
    },
    refuseF() {
      this.refuseDialog.currentData = {}
      this.refuseFrom.data = {}
      this.refuseDialog.show = false
    },
    bidTypeChange(val) {
      this.genName.typeName = val
      this.dialogFrom.data.bidName =
        (this.genName.dptName ? this.genName.dptName : '') +
        val +
        this.genName.code
    },
    refuseBeforeClose() {
      this.$refs['refuseFromRef'].resetFields()
    },
    // 需求确认
    confirm() {
      this.dialogText.title = '需求确认'
      this.dialogText.button.t = '确 定'
      this.dialogText.button.f = '取 消'
      this.dialogText.topFormShow = false
      this.dialogText.paginationShow = true
      this.dialogText.qeuryButtonShow = true
      this.dialogText.refuseShow = true
      this.dialogText.historyShow = true
      this.dialogText.submitTimeShow = true
      this.dialogText.show = true
      this.dialogText.selectBoxShow = true
      this.dialogText.sugarModeListShow = true
      this.dialogText.groupShow = true
      this.dialogText.sugarModeListName = '糖厂：'
    },
    clickEvent(val) {
      let code = val.mdlCode
      if (code === 'gen') {
        this.genDialog()
      }
      if (code === 'edit') {
        this.edit()
      }
      if (code === 'del') {
        this.del()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .app-container {
    overflow: hidden;
    background-color: #fff;
    position: relative;
    height: 100%;
  }

  .right-detail {
    height: 100%;

    .top-type {
      width: 100%;
      height: 100%;
      // .btn-area{
      //   margin-bottom: 10px;
      // }
      table {
        max-width: 100%;
        font-size: 1.4rem;
      }
    }

    .bottom-code {
      width: 100%;
      height: 100%;
    }
  }

  .codeNumberCode {
    font-size: 1.4rem;
    font-weight: 500;
    line-height: 40px;
  }

  .main-container {
    height: 100%;
    width: 100%;
    position: relative;
    // padding: 0 12px;
    display: flex;
    flex-direction: column;

    .form {
      width: 100%;
      flex: 0 0 auto;

      .row {
        width: 100%;
        // margin: 10px 0;
        margin-bottom: 0;
      }
    }
  }

  .basicInfoBox {
    border: 1px solid #cccccc;
    border-radius: 5px;
    padding-top: 10px;
  }

  .tableTitle {
    font-size: 15px;
    margin-top: 10px;
    padding-top: 10px;
    padding-bottom: 10px;
    font-weight: bold;
  }

  ::v-deep .splitter-pane-resizer.horizontal {
    margin-top: -15px;
  }
</style>
